package com.gm.lib.utils;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author haifei.zhang
 * @date 2013-9-9
 */
public class GMFormatUtil {


    /**
     * 格式化小数
     *
     * @param num
     * @return
     */
    public static String decimalFormat(Double num) {
        DecimalFormat df = new DecimalFormat(".00");
        String str = df.format(num);

        return str;
    }

    public static String decimalFormat(String num) {

        Double d = Double.valueOf(num);
        DecimalFormat df = new DecimalFormat(".00");
        String str = df.format(d);

        return str;
    }

    public static String decimalFormat(int num) {

        DecimalFormat df = new DecimalFormat(".00");
        String str = df.format(num);

        return str;
    }

    /**
     * 格式化为6位
     *
     * @param num
     * @return
     */
    public static String radomFormat(int num) {

        DecimalFormat df = new DecimalFormat("000000");
        String str = df.format(num);

        return str;
    }

    /**
     * 判断字符串中是否含有数字
     *
     * @param value
     * @return
     */
    public static boolean isContainsNum(String value) {
        Pattern pattern = Pattern.compile("\\d");
        Matcher matcher = pattern.matcher(value.substring(value.indexOf("/")));
        return matcher.find();

    }

    /**
     * 判断str字符串中包含element字符串的个数
     *
     * @return int  count
     */
    public static int getElementCount(String str, String element) {
        int count = 0;
        char c = element.charAt(0);
        for (int i = 0; i < str.length(); i++) {
            char tmp = str.charAt(i);
            if (Character.valueOf(c).equals(Character.valueOf(tmp))) {
                count++;
            }
        }
        return count;
    }
}
